# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>

_pkgname='intel-oneapi-compiler-shared'
pkgbase="${_pkgname}-runtime"
pkgname=("${_pkgname}-runtime" "${_pkgname}-runtime-libs")
pkgver=2023.2.0
_debpkgrel=49495
pkgrel=1
_pkgdesc="Intel oneAPI compiler runtime libraries"
arch=('loong64' 'x86_64')
url='https://software.intel.com/content/www/us/en/develop/tools/oneapi.html'
license=("custom")
depends=('level-zero-loader')
source=("https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb")
b2sums=('e81b4cf210cb628d81dd3490d80cb0ef4ba2fdb00ec26746c1564c1ace294d7fe4de7f6586d987b084c02000fd0095fae2e0806c2a40dcc59b16bc4fa17f3bce'
        'a34021b84db0c7dc46089502c5d003130ffd50d55aee86eeb72a041e5248f3b5d43c089650e7158e64ee0c572c80482a979edba5c6f6b4ce1622840084cfe055')

noextract=(
    "${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
)

prepare() {
    mkdir "${_pkgname}-${pkgver}"
    ar x "${srcdir}/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${_pkgname}-${pkgver}"
    rm data.tar.xz
    ar x "${srcdir}/${_pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${_pkgname}-${pkgver}"
    rm data.tar.xz
}

package_intel-oneapi-compiler-shared-runtime-libs() {
    depends=('intel-oneapi-common')
    conflicts=('intel-oneapi-basekit')
    pkgdesc="${_pkgdesc}: Minimal compiler libraries"

    local _lib_path='/opt/intel/oneapi/compiler'
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    local _bin
    for _bin in sycl-ls sycl-post-link; do
        cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin/${_bin}" \
            "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    done

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"
    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/compiler/include" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"

    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/compiler/lib" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler"

    # add latest and common symlink
    ln -s "${_lib_path}/${pkgver}" "${pkgdir}/${_lib_path}/latest"
    ln -s "${_lib_path}/latest/linux/compiler/lib/intel64_lin" \
          "${pkgdir}/${_lib_path}/${pkgver}/linux/compiler/lib/intel64"

    # allow libs to be found
    local _ldso_conf="${pkgdir}"/etc/ld.so.conf.d
    install -d "${_ldso_conf}"
    echo "${_lib_path}/latest/linux/compiler/lib/intel64_lin" >> "${_ldso_conf}/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
}

package_intel-oneapi-compiler-shared-runtime() {
    depends=('intel-oneapi-openmp' 'intel-oneapi-common' 'intel-oneapi-compiler-shared-runtime-libs')
    provides=("${_pkgname}-common")
    conflicts=('intel-oneapi-basekit')
    pkgdesc="${_pkgdesc}"

    local _lib_path='/opt/intel/oneapi/compiler'
    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux"
    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin-llvm" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux"

    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/include" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux"

    cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/lib" \
        "${pkgdir}/${_lib_path}/${pkgver}/linux"

    install -d "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    local _bin
    for _bin in aocl-ioc64 icx-cc icx-cl icx.cfg ioc64; do
        cp -a "${srcdir}/${_pkgname}-${pkgver}/${_lib_path}/${pkgver}/linux/bin/${_bin}" \
            "${pkgdir}/${_lib_path}/${pkgver}/linux/bin"
    done

    # allow libs to be found
    local _ldso_conf="${pkgdir}"/etc/ld.so.conf.d
    install -d "${_ldso_conf}"
    echo "${_lib_path}/latest/linux/lib" > "${_ldso_conf}/${pkgname}.conf"
    echo "${_lib_path}/latest/linux/lib/x64" >> "${_ldso_conf}/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
}

